package com.designoutput.utils
{	
	import flash.display.BitmapData;
	import flash.utils.ByteArray;
	
	import org.purepdf.elements.images.ImageElement;

	public class PDFUtils
	{
		public static function createTransparentImageElement( bitmap: BitmapData ): ImageElement
		{
			var output: ByteArray = new ByteArray();
			var transparency: ByteArray = new ByteArray();
			var input: ByteArray = bitmap.getPixels( bitmap.rect );
			input.position = 0;
			
			while( input.bytesAvailable ){
				const pixel: uint = input.readInt();
				
				// write the RGB informations
				output.writeByte( (pixel >> 16) & 0xff );
				output.writeByte( (pixel >> 8) & 0xff );
				output.writeByte( (pixel >> 0) & 0xff );
				
				// write the alpha informations
				transparency.writeByte( (pixel >> 24) & 0xff );
			}
			
			output.position = 0;
			transparency.position = 0;
			
			var mask: ImageElement = ImageElement.getRawInstance( bitmap.width, bitmap.height, 1, 8, transparency, null );
			var image: ImageElement = ImageElement.getRawInstance( bitmap.width, bitmap.height, 3, 8, output, null );
			
			if( bitmap.transparent )
			{
				mask.makeMask();
				image.imageMask = mask;
			}
			return image;
		}
	}
}